using System.Collections.Generic;
using RPG.Domain.Items;

namespace RPG.Domain.Character
{
    public class Equipment : ValueObject
    {
        public Item Weapon { get; }
        public Item Armor { get; }
        public Item Accessory { get; }

        public int StrengthBonus => CalculateStatBonus(stat => stat.Strength);
        public int AgilityBonus => CalculateStatBonus(stat => stat.Agility);
        public int IntelligenceBonus => CalculateStatBonus(stat => stat.Intelligence);
        public int HealthBonus => CalculateStatBonus(stat => stat.Health);
        public int ManaBonus => CalculateStatBonus(stat => stat.Mana);

        private Equipment(Item weapon, Item armor, Item accessory)
        {
            Weapon = weapon;
            Armor = armor;
            Accessory = accessory;
        }

        public static Equipment Empty()
        {
            return new Equipment(null, null, null);
        }

        public Equipment EquipItem(Item item)
        {
            switch (item.Type)
            {
                case ItemType.Weapon:
                    return new Equipment(item, Armor, Accessory);
                case ItemType.Armor:
                    return new Equipment(Weapon, item, Accessory);
                case ItemType.Accessory:
                    return new Equipment(Weapon, Armor, item);
                default:
                    throw new DomainException("Invalid equipment type");
            }
        }

        private int CalculateStatBonus(Func<ItemStats, int> statSelector)
        {
            var bonus = 0;
            if (Weapon?.Stats != null) bonus += statSelector(Weapon.Stats);
            if (Armor?.Stats != null) bonus += statSelector(Armor.Stats);
            if (Accessory?.Stats != null) bonus += statSelector(Accessory.Stats);
            return bonus;
        }
    }
}
